package com.school.vaccination.controller;

import com.github.pagehelper.PageInfo;
import com.school.support.base.BaseController;
import com.school.vaccination.domain.Child;
import com.school.vaccination.service.ChildService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import zhibi.common.support.base.Page;
import zhibi.common.support.exception.MessageException;
import zhibi.common.support.mybatis.example.Example;
import zhibi.common.support.mybatis.example.ExampleType;
import zhibi.common.support.mybatis.example.ExampleUtil;
import zhibi.common.util.DateUtil;

import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

/**
 * Created by 执笔 on 2017/3/18.
 */
@RequestMapping("child")
@Controller
public class ChildController extends BaseController {

    @Autowired
    private ChildService childService;

    /**
     * 小孩列表
     *
     * @param model
     * @param child
     * @param page
     * @return
     */
    @RequestMapping("list")
    public String list(Model model, Child child, Page page) {
        Example example = ExampleUtil.getInstance()
                .addParam("name", child.getName(), ExampleType.Operation.LIKE)
                .addParam("card", child.getCard(), ExampleType.Operation.LIKE)
                .addOrder("addtime", ExampleType.OrderType.DESC)
                .getExample();
        PageInfo<Child> pageInfo = childService.selectByExample(example, page);
        setModelAttribute(model, pageInfo);
        return "child/list";
    }

    /**
     * 跳转到添加小孩页面
     *
     * @return
     */
    @RequestMapping("add")
    public String add() {
        return "child/add";
    }

    /**
     * 添加小孩
     *
     * @param child
     * @return
     */
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public String save(Child child) {
        Child childTemp = new Child();
        childTemp.setCard(child.getCard());
        if (null != childService.selectOne(childTemp)) {
            throw new MessageException("该身份证已经注册过");
        }
        child.setAddtime(new Date());
        childService.insertSelective(child);
        return redirect("list" );
    }

    /**
     * 小孩详情
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("detail/{id}")
    public String detail(@PathVariable Integer id, Model model) {
        Child child = childService.selectByPK(id);
        if (null == child) {
            throw new MessageException("该小孩不存在");
        }
        model.addAttribute(child);
        return "child/detail";
    }

    /**
     * 更新小孩信息
     *
     * @param child
     * @return
     */
    @RequestMapping("update")
    public String uopdate(Child child) {
        Child childTemp = new Child();
        childTemp.setCard(child.getCard());
        childTemp = childService.selectOne(childTemp);
        if (null != childTemp && !Objects.equals(child.getId(), childTemp.getId())) {
            throw new MessageException("该身份证已经注册过");
        }
        childService.updateByIdSelective(child);
        return redirect("list" );
    }
}
